<template>
    <div id="userLayout" :class="['user-layout-wrapper', isMobile && 'mobile']">
      <div class="container">
        <div class="user-layout-lang">
          <!-- <select-lang class="select-lang-trigger" /> -->
        </div>
        <div class="user-layout-content">
          <div class="top">
            <div class="header">
              <a href="/">
                <img :src="logo" class="logo" alt="logo" />
              </a>
            </div>
            <div class="desc">智能化专业知识考试系统管理后台</div>
          </div>
  
          <div class="main">
            <el-form
              ref="formRef"
              :model="form"
              :rules="formRules"
              class="login-form"
              size="large"
            >
              <el-form-item label="" prop="loginName">
                <el-input
         
                  v-model.trim="form.loginName"
                  placeholder="请输入账号"
                />
              </el-form-item>
              <el-form-item label="" prop="pwd">
                <el-input
           
                  v-model.trim="form.pwd"
                  type="password"
                  show-password
                  placeholder="请输入密码"
                />
              </el-form-item>
              <el-form-item>
                <div class="loginBtn" @click="login">登陆</div>
              </el-form-item>
            </el-form>
          </div>
  
          <div class="footer">
           <Footer />
          </div>
        </div>
      </div>
    </div>
  </template>
    
  <script setup lang="ts">
  import { onMounted, onUnmounted, reactive, ref } from "vue";
  import type { FormInstance, FormRules } from "element-plus";
  import { useRouter } from "vue-router";
  import Footer from "@/components/footer.vue";
  import http from "@/request/index";
  import { useUserStore } from "@/stores/user";
  import { useDictStore } from "@/stores/dict";
  import logo from "@/assets/image/logo.png";
  // 定义变量
  const router = useRouter();
  const form = reactive({
    // 表单
    loginName: "",
    pwd: "",
  });
  const formRef = ref<FormInstance>(); // 表单引用
  const formRules = reactive<FormRules>({
    // 表单规则
    loginName: [{ required: true, message: "请输入账号", trigger: "blur" }],
    pwd: [
      { required: true, message: "请输入密码", trigger: "blur" },
      {
        //插入正则验证：大小写、数字、至少8位、不常用字符
        pattern:
          /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@#!%^*?&+-.])[A-Za-z\d$@#!%^*?&+-.]{8,}/,
        message: "密码应当至少8位且含有数字、大小写字母及特殊字符",
      },
    ],
  });
  const keyDown = (e: KeyboardEvent) => {
    if (e.keyCode == 13) {
      login();
    }
  };
  
  // 组件挂载完成后，执行如下方法
  onMounted(() => {
    window.addEventListener("keydown", keyDown);
    form.loginName = localStorage.getItem("loginName");
    form.pwd = localStorage.getItem("pwd");
  });
  
  // 组件被卸载之后，执行如下方法
  onUnmounted(() => {
    window.removeEventListener("keydown", keyDown, false);
  });
  
  // 登录
  async function login() {
    // 校验数据有效性
    if (!formRef.value) return;
    await formRef.value.validate(async (valid, fields) => {
      if (!valid) {
        return;
      }
  
      // 登录
      let {
        data: { code, data },
      } = await http.post("login/managerIn", {
        loginName: form.loginName,
        pwd: form.pwd,
      });
      if (code !== 200) {
        return;
      }
  
      // 缓存令牌
      const userStore = useUserStore();
      userStore.id = data.userId;
      userStore.name = data.userName;
      userStore.type = data.type;
      userStore.accessToken = data.accessToken;
  
      // 缓存数据字典
      let {
        data: { data: dictData },
      } = await http.post("dict/listpage", {
        pageSize: 100,
      });
      const dictStore = useDictStore(); // 字典缓存
      dictStore.dicts = dictData.list;
      localStorage.setItem("loginName", form.loginName);
      localStorage.setItem("pwd", form.pwd);
      // 跳转到工作台
      router.push("/home");
    });
  }
  </script>
    
    <style lang="scss" scoped>
  .login-form {
    position: relative;
    height: auto;
    box-sizing: border-box;
    margin-bottom: 100px;
    :deep .el-input--large .el-input__wrapper {
      height: 50px;
    }
    :deep .el-form-item__content{
      display: flex;
      justify-content: center;
    }
     .loginBtn{
      margin-top: 10px;
      width: 400px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 6px;
      background: linear-gradient(to right, #24b0fb, #1c90fa);
      color: #fff;
      font-size: 18px;
      cursor: pointer;
      &:hover{
          background-color: #46a6ff;
      }
    }
  
    .login-form-title {
      font-size: 22px;
      font-weight: bold;
      color: var(--el-text-color-regular);
      padding-bottom: 20px;
      display: flex;
      align-items: center; /* 这行代码使内容垂直居中 */
    }
  
    .el-button {
      width: 100%;
      height: 45px;
    }
  }
  #userLayout.user-layout-wrapper {
    height: 100vh;
    overflow-x: hidden;
  
    &.mobile {
      .container {
        .main {
          max-width: 368px;
          width: 98%;
        }
      }
    }
  
    .container {
      width: 100%;
      height: 100%;
      background: #f0f2f5 url(@/assets/image/background.svg) no-repeat 50%;
      background-size: 100%;
      //padding: 50px 0 84px;
      position: relative;
  
      .user-layout-lang {
        width: 100%;
        height: 40px;
        line-height: 44px;
        text-align: right;
  
        .select-lang-trigger {
          cursor: pointer;
          padding: 12px;
          margin-right: 24px;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          font-size: 18px;
          vertical-align: middle;
        }
      }
  
      .user-layout-content {
        padding: 32px 0 24px;
  
        .top {
          text-align: center;
  
          .header {
            height: 44px;
            line-height: 44px;
  
            .badge {
              position: absolute;
              display: inline-block;
              line-height: 1;
              vertical-align: middle;
              margin-left: -12px;
              margin-top: -10px;
              opacity: 0.8;
            }
  
            .logo {
              height: 66px;
              // vertical-align: top;
              margin-right: 16px;
              border-style: none;
            }
  
            .title {
              font-size: 33px;
              color: rgba(0, 0, 0, 0.85);
              font-family: Avenir, "Helvetica Neue", Arial, Helvetica, sans-serif;
              font-weight: 600;
              position: relative;
              top: 2px;
            }
  
          }
  
          .desc {
            font-size: 30px;
            color: #333;
            margin-top: 60px;
            margin-bottom: 40px;
          }
        }
  
        .main {
          min-width: 260px;
          width: 400px;
          margin: 0 auto;
        }
  
        .footer {
          // position: absolute;
          width: 100%;
          bottom: 0;
          padding: 0 16px;
          margin: 48px 0 24px;
          text-align: center;
  
          .links {
            margin-bottom: 8px;
            font-size: 14px;
  
            a {
              color: rgba(0, 0, 0, 0.45);
              transition: all 0.3s;
  
              &:not(:last-child) {
                margin-right: 40px;
              }
            }
          }
  
          .copyright {
            color: rgba(0, 0, 0, 0.45);
            font-size: 14px;
          }
        }
      }
  
      a {
        text-decoration: none;
      }
    }
  }
  </style>
  